package m3f.retrieval;

import m3f.backprojection.Combiner;
import m3f.computing.ConcurrentTemplate;
import m3f.data.MultimodalDataProvider;
import m3f.matrix.Minibatch;

import org.jblas.DoubleMatrix;

public class ConcurrentBackProjectedRank extends ConcurrentTemplate {
	
	private MultimodalDataProvider database;
	private DoubleMatrix queriesbp;
	private Rank.SimilarityMeasure measure;
	public DoubleMatrix R;
	private double alpha;
	private DoubleMatrix P;
	
	public ConcurrentBackProjectedRank(MultimodalDataProvider database, DoubleMatrix queriesbp, DoubleMatrix P, double alpha, Rank.SimilarityMeasure measure){
		super(database.matrixRows()/database.getMinibatchSize()+1);
		this.database = database;
		this.queriesbp = queriesbp;
		this.measure = measure;
		this.alpha = alpha;
		this.P = P;
		BackProjectedRank.initMatrix();
	}

	@Override
	public void singleOperation(int reg) {
		Minibatch dbv;
		Minibatch dbh;
		synchronized(this.database){
			dbv = this.database.nextVisual();  
			dbh = this.database.nextTextual(); 
		}
		if (dbv != null){
			System.out.println("Minibatch " + dbv.getId());
			DoubleMatrix dbbp = Combiner.backprojection(dbv, dbh, P, alpha);
			BackProjectedRank.rank(dbbp, queriesbp, measure, dbv.getId());		
		}

	}
	
}
